package com.lyq.redis;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.concurrent.TimeUnit;

/**
 *
 */
@Slf4j
public class RedisTest2 {
    public static void main(String[] args) {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
//        config.useSingleServer().setPassword("123456");
        config.useSingleServer().setConnectionPoolSize(1000);
        config.useSingleServer().setConnectionMinimumIdleSize(100);
        RedissonClient redissonClient = Redisson.create(config);

        RLock rLock = redissonClient.getLock("store_lock_cn_order");
        rLock.lock(30, TimeUnit.SECONDS);   //阻塞，直到key过期
//        log.info("获取锁成功-RedisTest2");

//        log.info("执行完毕-RedisTest2");
        try {
            boolean tryLock1 = rLock.tryLock(0, 30, TimeUnit.SECONDS);//直接返回
            log.info("tryLock1:{}", tryLock1); 

            boolean tryLock2 = rLock.tryLock(60, 30, TimeUnit.SECONDS);//根据waitTime阻塞
            log.info("tryLock2:{}", tryLock2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
